package com.atguigu.lease.app.service.impl;

import com.atguigu.lease.app.mapper.BrowsingHistoryMapper;
import com.atguigu.lease.app.mapper.GraphInfoMapper;
import com.atguigu.lease.app.service.BrowsingHistoryService;
import com.atguigu.lease.app.service.GraphInfoService;
import com.atguigu.lease.common.handler.StgyAppAuthInterceptor;
import com.atguigu.lease.model.bean.BrowsingHistory;
import com.atguigu.lease.model.bean.GraphInfo;
import com.atguigu.lease.model.enums.ItemTypeEnum;
import com.atguigu.lease.model.vo.GraphVo;
import com.atguigu.lease.model.vo.app.HistoryItemVo;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author 1
* @description 针对表【browsing_history(浏览历史)】的数据库操作Service实现
* @createDate 2025-06-20 14:04:43
*/
@Service
public class BrowsingHistoryServiceImpl extends ServiceImpl<BrowsingHistoryMapper, BrowsingHistory>
    implements BrowsingHistoryService{
    @Autowired
    GraphInfoMapper graphInfoMapper;
    @Async //标注的方法，如果启动类开启了异步，会异步执行!
    @Override
    public void saveBrowsingHistoryAsyc(Long roomId, Long userId) {
//        try {
//            Thread.sleep(20000);
//        } catch (InterruptedException e) {
//            throw new RuntimeException(e);
//        }
        //sprig-context包： 提供了定时任务、还有异步任务(自动通过子线程执行业务代码)
        //将用户浏览当前房间的记录保存到浏览记录表中！
        BrowsingHistory history = new BrowsingHistory();
        history.setRoomId(roomId);
        history.setBrowseTime(new Date());
        // 除了登录相关的接口拦截器放行了，其他接口访问时都会被拦截器拦截 校验登录状态
        //  然后将登录状态通过ThreadLocal保存，后面可以获取使用！
        history.setUserId(userId);
        this.save(history);
    }

    @Override
    public Page<HistoryItemVo> pageItem(Long current, Long size) {
        Page<HistoryItemVo> page = baseMapper.selectPageItem(new Page<HistoryItemVo>(current, size),
                StgyAppAuthInterceptor.APP_USER_THREAD_LOCAL.get().getId());
        //如果只是遍历集合 获取某个属性值 或者修改某些属性值，使用for遍历
        page.getRecords().forEach(historyItemVo -> {
            //根据房间id查询他的图片列表
            List<GraphVo> graphVoList = graphInfoMapper.selectList(Wrappers.lambdaQuery(GraphInfo.class)
                            .eq(GraphInfo::getItemId, historyItemVo.getRoomId())
                            .eq(GraphInfo::getItemType, ItemTypeEnum.ROOM)
                            .select(GraphInfo::getName, GraphInfo::getUrl))
                    .stream()
                    .map(graphInfo -> {
                        GraphVo graphVo = new GraphVo();
                        BeanUtils.copyProperties(graphInfo, graphVo);
                        return graphVo;
                    }).collect(Collectors.toList());
            historyItemVo.setRoomGraphVoList(graphVoList);
        });
        //如果需要将集合中的对象转为另一个类型的对象集合 需要stream().map()
        return page;
    }
}




